import http from './http'
import type { ApiResponse, ResumeDetailVO, WorkExperienceVO, SkillVO, ProjectVO } from '@/types/api'

export async function fetchResume() {
  const resp = await http.get<ApiResponse<ResumeDetailVO>>('/resume')
  return resp.data
}

// 获取当前登录用户的简历
export async function getMyResume() {
  const resp = await http.get<ApiResponse<ResumeDetailVO>>('/resume/me')
  return resp.data
}

// 保存/更新当前用户简历基本信息
export async function upsertMyResume(payload: Partial<ResumeDetailVO>) {
  const body = {
    name: payload.name,
    title: payload.title,
    summary: payload.summary,
    avatar: payload.avatar,
    email: payload.email,
    phone: payload.phone,
    location: payload.location,
    github: payload.github,
    linkedin: payload.linkedin,
    website: payload.website
  }
  const resp = await http.put<ApiResponse<boolean>>('/resume', body)
  return resp.data
}

// 经验 CRUD
export async function addExperience(vo: Omit<WorkExperienceVO, 'id' | 'resumeId'>) {
  const resp = await http.post<ApiResponse<number>>('/resume/experiences', vo as any)
  return resp.data
}

export async function updateExperience(id: number, vo: Partial<WorkExperienceVO>) {
  const resp = await http.put<ApiResponse<boolean>>(`/resume/experiences/${id}`, vo as any)
  return resp.data
}

export async function deleteExperience(id: number) {
  const resp = await http.delete<ApiResponse<boolean>>(`/resume/experiences/${id}`)
  return resp.data
}

// 技能 CRUD
export async function addSkill(vo: Omit<SkillVO, 'id' | 'resumeId'>) {
  const resp = await http.post<ApiResponse<number>>('/resume/skills', vo as any)
  return resp.data
}

export async function updateSkill(id: number, vo: Partial<SkillVO>) {
  const resp = await http.put<ApiResponse<boolean>>(`/resume/skills/${id}`, vo as any)
  return resp.data
}

export async function deleteSkill(id: number) {
  const resp = await http.delete<ApiResponse<boolean>>(`/resume/skills/${id}`)
  return resp.data
}

// 项目 CRUD
export async function addProject(vo: Omit<ProjectVO, 'id' | 'resumeId'>) {
  const resp = await http.post<ApiResponse<number>>('/resume/projects', vo as any)
  return resp.data
}

export async function updateProject(id: number, vo: Partial<ProjectVO>) {
  const resp = await http.put<ApiResponse<boolean>>(`/resume/projects/${id}`, vo as any)
  return resp.data
}

export async function deleteProject(id: number) {
  const resp = await http.delete<ApiResponse<boolean>>(`/resume/projects/${id}`)
  return resp.data
}


